# coding: utf-8
from typing import List

from PySide6.QtCore import Signal
from PySide6.QtWidgets import QHBoxLayout
from qfluentwidgets import MessageBoxBase, BodyLabel, VerticalSeparator, FluentIcon as FIF, setFont

from ..widgets import ListWidget, ElLineEdit, CustomSimpleCardWidget, ToolTipButton, Alert


class ItemWidget(CustomSimpleCardWidget):
    deleteSignal = Signal(object)

    def __init__(self, parent=None):
        super().__init__(parent)
        self.horizontalLayout = QHBoxLayout(self)
        self.keyEdit = ElLineEdit(self)
        self.valueEdit = ElLineEdit(self)
        self.deleteBtn = ToolTipButton.transparentToolButton(self, FIF.CLOSE, '删除')

        self.keyEdit.setPlaceholderText('键')
        self.valueEdit.setPlaceholderText('值')
        self.keyEdit.setClearButtonEnabled(True)
        self.valueEdit.setClearButtonEnabled(True)

        self.horizontalLayout.addWidget(self.keyEdit)
        self.horizontalLayout.addWidget(VerticalSeparator(self))
        self.horizontalLayout.addWidget(self.valueEdit)
        self.horizontalLayout.addWidget(self.deleteBtn)
        self.setFixedHeight(50)
        self.deleteBtn.clicked.connect(lambda: self.deleteSignal.emit(self))

    def key(self):
        return self.keyEdit.text()

    def value(self):
        return self.valueEdit.text()

    def setKey(self, key: str):
        self.keyEdit.setText(key)

    def setValue(self, value: str):
        self.valueEdit.setText(value)

    def params(self):
        key = self.key()
        value = self.value()
        return {key: value}


class HeadersDialog(MessageBoxBase):
    def __init__(self, title: str = '标题', parent=None):
        super().__init__(parent)
        self.widgets = []  # type: List[ItemWidget]
        self.horizontalLayout = QHBoxLayout()
        self.titleLabel = BodyLabel(title, self)
        self.addBtn = ToolTipButton.transparentToolButton(self, FIF.ADD, '添加')
        self.listWidget = ListWidget(self)

        setFont(self.titleLabel, 20)
        self.widget.setMinimumWidth(500)
        self.horizontalLayout.addWidget(self.titleLabel)
        self.horizontalLayout.addWidget(self.addBtn)
        self.viewLayout.addLayout(self.horizontalLayout)
        self.viewLayout.addWidget(self.listWidget)

        self.addBtn.clicked.connect(self.addNewItem)

    def addNewItem(self):
        item = ItemWidget(self.listWidget)
        self.widgets.append(item)
        item.deleteSignal.connect(self.removeWidget)
        self.listWidget.addWidget(item)

    def removeWidget(self, item: ItemWidget):
        self.widgets.remove(item)
        self.listWidget.removeWidget(item)

    def params(self):
        params = {}
        for widget in self.widgets:
            params.update(widget.params())
        return params

    def validate(self) -> bool:
        try:
            params = self.params()
            if not params or not all(params.keys()):
                Alert.warning('参数不能为空')
                return False
        except ValueError:
            return False
        return True

    @classmethod
    def showDialog(cls, title: str = '标题', parent=None):
        dialog = cls(title, parent)
        _ = dialog.exec()
        params = dialog.params()
        dialog.deleteLater()
        return _, params
